﻿#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QThread>
#include <QRunnable>
#include <QTcpSocket>
#include <QJsonObject>
#include <QFile>

class MyThread : public QThread, public QRunnable
{
    Q_OBJECT
public:
    enum TransferType
    {
        Upload,
        Download
    };

    explicit MyThread(TransferType type,
                      const QString &fromname,
                      const QString &filepath,
                      const QString &filename,
                      const QString button,
                      const int startChunk,
                      const QString &host,
                      quint16 port,
                      QThread *parent = nullptr);

    ~MyThread();

    void run();

    void WriteToServer(const QJsonObject &obj);

    // 处理json逻辑
    void process(const QByteArray &data);

signals:
    void progressChanged(int chunkIndex);
    void showload();
    void showInfo(const QString &filename);

public slots:
    void onConnected();

private:
    QTcpSocket *socket;
    TransferType transferType;
    QString fromName;
    QString filePath;
    QString fileName;
    QString Button;
    int startChunk;
    QFile file;
    const int chunkSize = 1024 * 1024;
    qint64 fileSize;
    int totalChunks;
    QByteArray buffer; // 自定义缓冲区
    bool isConnected;
    bool flag;
};

#endif // MYTHREAD_H
